//
//  CustomTabBarController.m
//  MexicoLoan
//
//  Created by WXP on 2025/2/24.
//
//

#import "CustomTabBarController.h"
#import "WXPHomePageVC.h"
#import "WXPOrderVC.h"
#import "WXPMineVC.h"
#import "CustomTabBar.h"



@interface CustomTabBarController ()

@end

@implementation CustomTabBarController

#pragma mark - viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置自定义TabBar
    [self setupCustomTabBar];
    
    // 添加子控制器
    [self setupChildViewControllers];
}

#pragma mark - viewDidLayoutSubviews
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    CGFloat tabBarWidth = 240;
    CGFloat tabBarHeight = 50;
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    CGFloat x = (screenWidth - tabBarWidth) / 2;
    
    CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
    CGFloat y = screenHeight - tabBarHeight - ([GlobalMethod getBottomSafeAreaHeight] == 0 ? LEFT_MARDIN : [GlobalMethod getBottomSafeAreaHeight]);
    
    self.tabBar.frame = CGRectMake(x, y, tabBarWidth, tabBarHeight);
}

#pragma mark - Private Methods
- (void)setupCustomTabBar {
    CustomTabBar *customTabBar = [[CustomTabBar alloc] init];
    
    CGFloat tabBarWidth = 240;
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    CGFloat x = (screenWidth - tabBarWidth) / 2;
    CGRect frame = self.tabBar.frame;
    frame.size.width = tabBarWidth;
    frame.origin.x = x;
    customTabBar.frame = frame;
    
    [self setValue:customTabBar forKey:@"tabBar"];
}

- (void)setupChildViewControllers {
    // 首页
    WXPHomePageVC *homeVC = [[WXPHomePageVC alloc] init];
    [self setupChildVC:homeVC title:@"" imageName:@"icon-tabbar-home-normal" selectedImageName:@"icon-tabbar-home-select"];
    
    // 订单
    WXPOrderVC *orderVC = [[WXPOrderVC alloc] init];
    [self setupChildVC:orderVC title:@"" imageName:@"icon-tabbar-order-normal" selectedImageName:@"icon-tabbar-order-select"];
    
    // 我的
    WXPMineVC *mineVC = [[WXPMineVC alloc] init];
    [self setupChildVC:mineVC title:@"" imageName:@"icon-tabbar-me-normal" selectedImageName:@"icon-tabbar-me-select"];
}

- (void)setupChildVC:(UIViewController *)vc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName {
    vc.tabBarItem.title = title;
    vc.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor grayColor]} forState:UIControlStateNormal];
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blueColor]} forState:UIControlStateSelected];
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    nav.navigationBarHidden = YES;
    [self addChildViewController:nav];
}

@end
